package com.itany.corejava.day12;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class TestJTable {

    private JFrame jf;
    private JTable jt;
    private List<User> users;

    public TestJTable(List<User> users) {
        jf=new JFrame();

        this.users=users;


//        TableModel model=new UserTableModel();

        DefaultTableModel model=new DefaultTableModel();

        model.addColumn("id");
        model.addColumn("username");
        model.addColumn("age");


//        for (User user : users) {
//            Vector<Object> rowData=new Vector<>();
//            rowData.add(user.getId());
//            rowData.add(user.getUsername());
//            rowData.add(user.getAge());
//            model.addRow(rowData);
//        }


        users.forEach(user -> {
            Vector<Object> rowData=new Vector<>();
            rowData.add(user.getId());
            rowData.add(user.getUsername());
            rowData.add(user.getAge());
            model.addRow(rowData);
        });


        jt=new JTable(model);
    }

    private void init(){
        JScrollPane jsp=new JScrollPane(jt);
        jf.add(jsp);
    }

    public void show(){
        init();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }





    private class UserTableModel extends AbstractTableModel {

        @Override
        public int getRowCount() {
            return users.size();
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            User user=users.get(rowIndex);
            switch (columnIndex){
                case 0:
                    return user.getId();
                case 1:
                    return user.getUsername();
                case 2:
                    return user.getAge();
            }
            return null;
        }

        @Override
        public String getColumnName(int column) {
            switch (column){
                case 0:
                    return "id";
                case 1:
                    return "username";
                case 2:
                    return "age";
            }

            return null;
        }
    }




    public static void main(String[] args) {


        List<User> users=new ArrayList<>();

        users.add(new User(1,"aaa",21));
        users.add(new User(2,"bbb",23));
        users.add(new User(3,"ccc",24));
        users.add(new User(4,"ddd",22));
        users.add(new User(5,"eee",27));
        users.add(new User(6,"fff",23));
        users.add(new User(7,"ggg",26));
        users.add(new User(8,"hhh",21));
        users.add(new User(9,"iii",27));


        new TestJTable(users).show();
    }
}
